import 'package:aio/aio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:app/generated/app_strings.dart';
import 'package:app/src/http/bean/wroks/attachment_bean.dart';
import 'package:app/src/http/bean/wroks/contract_bean.dart';
import 'package:app/src/http/bean/wroks/customers_copyInfo.dart';
import 'package:app/src/http/bean/wroks/product_bean.dart';
import 'package:app/src/ui/app_ui.dart';
import 'package:app/src/ui/route/helper/route_helper.dart';
import 'package:app/src/ui/route/works/public/items/ui_base.dart';
import 'package:app/src/ui/route/works/public/items/ui_card.dart';
import 'package:app/src/utils/image_path_utils.dart';

import '../../../../../http/bean/works_2023/contractroyaltyrateapplycheck_bean.dart';
import '../../../../dialog/dailog.dart';

///提成比例审核-》合同信息 -》送货路线
class UiContractFreightInfoList {
  static Widget build(BuildContext context, List<ContractFreightInfo>? data,
      List<ContractFreightInfo>? oldData) {
    if (StringUtils.isEmpty(data)) {
      return AppUi.getEmptyView();
    }
    StateSetter? _setState = null;
    return StatefulBuilder(builder: (context, setState) {
      _setState = setState;
      List<Widget> children = [];
      children.add(UiBase.title(context, AppStrings.of(context).songhuoluxiang,
          bottom: AppEdgeInsets.margin_bottom)); //标题
      ///产品数据表格
      //children.add(_table(context, data, oldData));
      children.add(_table(context, data, null));
      var column = UiBase.column(context, children);
      var card = UiCard.build(context, column);
      return card;
    });
  }

  static Widget _table(BuildContext context, List<ContractFreightInfo>? data,
      List<ContractFreightInfo>? oldData) {
    List<TableRow> children = [];
    //标题
    children.add(TableRow(
        //第一行，添加背景色
        decoration:
            BoxDecoration(color: AppColors.backgroundColorWithTableTitle),
        children: [
          Center(
            child: _text(AppStrings.of(context).TheSerial), //序号
          ),
          Center(
            child: _text(AppStrings.of(context).fahuodizhi), //发货地址
          ),
          Center(
            child: _text(AppStrings.of(context).shouhuodizhi), //收货地址
          ),
          Center(
            child: _text(AppStrings.of(context).lucheng), //路程
          )
        ]));
    var serial = 1;
    if (data != null && data.length > 0) {
      for (int i = 0; i < data.length; i++) {
        ContractFreightInfo element = data[i];
        ContractFreightInfo? mOldData = null;
        if (oldData != null && oldData.length > i) {
          mOldData = oldData?[i];
        }
        //内容
        children.add(TableRow(
            //第一行，添加背景色
            children: [
              Center(
                child: _text((serial++).toString()), //序号
              ),
              Center(
                child: _text(element.FactoryName), //发货地址
              ),
              Center(
                child: _text(element.Address), //收货地址
              ),
              Center(
                child: _text(element.Distance), //路程
              ),
            ]));
      }
    }
    var table = Table(
      defaultVerticalAlignment: TableCellVerticalAlignment.middle,
      columnWidths: {
        0: FixedColumnWidth(50),
        1: FlexColumnWidth(2),
        2: FlexColumnWidth(2.5),
      },
      border: AppStyle.getTableBorder(),
      textDirection: TextDirection.ltr,
      children: children,
    );
    var width = 800.0;
    if (width < px.screenWidth()) {
      width = px.screenWidth() - 60;
    }
    return SingleChildScrollView(
      scrollDirection: Axis.horizontal,
      physics: BouncingScrollPhysics(),
      child: Container(
        width: width,
        child: table,
      ),
    );
  }

  static Widget _text(String? text, {bool isBlue = false}) {
    TextStyle? style = null;
    if (isBlue) {
      style = AppStyle.textStyleWithCardBlue();
    } else {
      style = AppStyle.textStyleWithCard();
    }
    return Text(
      text ?? "",
      style: style,
    );
  }
}
